<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Product extends FrontEndController {
    public function __construct() {
        parent::__construct();
    }

    private function intialPaging(){
        $option = array(
            'a.deleted_at' => null,
            'a.active_flg' => ON,
            'b.deleted_at' => null,
            'c.deleted_at' => null,
            'd.deleted_at' => null,
            'e.deleted_at' => null,
            'e.active_flg' => ON
        );
        $this->mypaging->setCount($this->product_model->countAllProduct($option));
        $this->mypaging->setLimit(PRODUCT_TOP_LIMIT);
        $this->mypaging->setMainURL(product_paging_url());
        $this->mypaging->setSuffix($this->config->item('url_suffix'));
    }

    public function index($pageIndex=1){
        $this->load->model('repo/product_model', 'product_model');
        $this->load->library('mypaging');
        $this->intialPaging();
        $offset    = ($pageIndex-1) * PRODUCT_TOP_LIMIT;
        $pagingBar = $this->mypaging->getBar($pageIndex);

        $option = array(
            'a.deleted_at' => null,
            'a.active_flg' => ON,
            'b.deleted_at' => null,
            'c.deleted_at' => null,
            'd.deleted_at' => null,
            'e.deleted_at' => null,
            'e.active_flg' => ON
        );
        $aProduct = $this->product_model->getProductList(PRODUCT_TOP_LIMIT, $offset, $option);
        $aReturn = array(
            'pageTitle' => 'Sản phẩm',
            'aProduct'  => $aProduct,
            'pagingBar' => $pagingBar
        );
        if($pageIndex > 1){
            $aReturn['pageTitle'] = 'Trang ' . $pageIndex . ' | Sản phẩm';
        }
        $this->renderView('pages/product', $aReturn);
    }

    public function getProduct($slug) {
        $this->load->model('repo/product_model', 'product_model');
        $this->load->model('repo/shop_model',    'shop_model');
        $this->load->model('repo/product_sale_model', 'product_sale_model');
        $product = $this->product_model->getProduct($slug);
        if(count($product)==0){ show_404(); }
        $details    = $this->product_model->getProductDetails($product->product_id);
        $shop       = $this->shop_model->getShop($product->product_shop_id);
        $shop_sales = $this->product_sale_model->getShopSale($product->product_shop_id);
        $aReturn    = array(
            'pageTitle'      => $product->product_title,
            'product'        => $product,
            'product_detail' => $details,
            'product_sales'  => $shop_sales,
            'shop'           => $shop
            );
        $this->renderView('pages/product-single', $aReturn);
    }
}
?>